package com.alinesno.cloud.base.storage.aop;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alinesno.cloud.base.storage.entity.StorageRequestsEntity;
import com.alinesno.cloud.base.storage.service.IStorageRequestsService;

/**
 * 记录系统接口记录
 * @author WeiXiaoJin
 * @since 2019年4月8日 下午8:30:43
 */
@Order(5)
@Aspect
@Component
public class ApiRecordAspect {
	
    @SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(ApiRecordAspect.class);

    @Autowired
    private IStorageRequestsService requestsService ; 
    
    @Pointcut("@annotation(com.alinesno.cloud.base.storage.aop.ApiRecord)")  
    public void pointcut(){
    }
 
    //统计请求的处理时间
    ThreadLocal<Long> startTime = new ThreadLocal<>();
   
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) {
        Object result = null;
        long beginTime = System.currentTimeMillis();
        try {
            // 执行方法
            result = point.proceed();
        } catch (Throwable e) {
        	e.printStackTrace();
        }
        // 执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        
        // 保存日志
        monitorAccountOperator(point, time , result);
        
        return result;
    }

    /**
     * 记录用户操作日志
     * @param joinPoint
     * @param time
     */
    @Async
	public void monitorAccountOperator(ProceedingJoinPoint joinPoint, long time , Object result) {
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes() ; 
        HttpServletRequest request = attributes.getRequest(); 
        String agent = request.getHeader("User-Agent");
        String url = request.getRequestURL().toString() ; 
        
        String applicationName = request.getParameter("applicationName") ; 
        String userId = request.getParameter("userId") ; 
        String userName = request.getParameter("userName") ; 
        String fileType = request.getParameter("suffix") ; 
        String filename = request.getParameter("filename") ; 
        
    	StorageRequestsEntity e = new StorageRequestsEntity() ; 
 
    	e.setAgent(agent); 
    	e.setRequestUrl(url);
    	e.setRequestUserId(userId);
    	e.setRequestUsername(userName);
    	e.setFilename(filename);
    	e.setRequestType(fileType);
    	e.setRequestApplication(applicationName);
    	e.setResponseTime(time);
    	e.setFieldProp(result+"");
    	e.setAddTime(new Date());
    	
    	requestsService.save(e) ; 
    }
 
}


